package it.unipd.dei.project.iandroid;


import java.util.ArrayList;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ExpandableListView;

/**
 * 
 * Activity contenente la lista delle partite ordinate per data
 *
 */
public class Calendar extends Activity {
	
	DatabaseOpenHelper db;
	SQLiteDatabase base;
	Cursor cur;
	Cursor cur2;
	Cursor cur3;
	int luogo = 0;
	int annoPartita;
	int mesePartita;
	int giornoPartita;
	private ExpandListAdapter ExpAdapter;
	private ArrayList<ExpandListGroup> ExpListItems;
	private ExpandableListView ExpandList;
	int selected;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar_list);
		
		db = new DatabaseOpenHelper(this);
		base = db.getWritableDatabase();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		setContentView(R.layout.calendar_list);
		
		db = new DatabaseOpenHelper(this);
		base = db.getWritableDatabase();
		cur = base.query("Partita", null, null, null, "Data", null, "Data ASC");
		if(cur.getCount()>0) {
			ExpandList = (ExpandableListView) findViewById(R.id.expandableListView1);
			ExpListItems = SetStandardGroups();
			ExpAdapter = new ExpandListAdapter(this, ExpListItems);
			ExpandList.setAdapter(ExpAdapter);
			ExpandList.setGroupIndicator(null);
		}
		
	}
	
	
	public ArrayList<ExpandListGroup> SetStandardGroups() {
			        ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
			        ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
			       
			        GregorianCalendar dataAttuale = new GregorianCalendar();
			        int annoAttuale = dataAttuale.get(GregorianCalendar.YEAR);
			        int meseAttuale = dataAttuale.get(GregorianCalendar.MONTH) + 1; // i mesi partono da 0
			        int giornoAttuale = dataAttuale.get(GregorianCalendar.DATE);
			        
			        GregorianCalendar oggi = new GregorianCalendar(annoAttuale, meseAttuale, giornoAttuale);
			        System.err.println(annoAttuale +" "+ meseAttuale +" "+ giornoAttuale);
					
			        for (int i = 0; i < cur.getCount(); i++) {
						cur.moveToNext();
						
						annoPartita = Integer.parseInt(cur.getString(8).substring(0,4));
		        		mesePartita = Integer.parseInt(cur.getString(8).substring(5,7));
		        		giornoPartita = Integer.parseInt(cur.getString(8).substring(8,10));
		        		
		        		GregorianCalendar dataPartita = new GregorianCalendar(annoPartita, mesePartita, giornoPartita);
		                //in lista nel calendario solo se la data della partita >= oggi
		        		
		        		if(dataPartita.compareTo(oggi)>=0){
		        		   ExpandListGroup gru1 = new ExpandListGroup();
				           gru1.setName(cur.getString(8));
				        
				           cur2 = base.query("Partita", null, null, null, null, null, "Data ASC");
				        	
				           for(int j = 0; j< cur2.getCount(); j++) {
				        		cur2.moveToNext();
				        		
				        		if(cur.getString(8).equalsIgnoreCase(cur2.getString(8))) {
				        			luogo = cur2.getInt(7);
				        			ExpandListChild ch1_1 = new ExpandListChild();
				        			cur3 = base.query("Team", null, "Id="+cur2.getInt(4), null, null, null, null);
				        			cur3.moveToFirst();
				        			if(luogo == 1)
				        			ch1_1.setName(cur2.getString(1)+"\n"+cur3.getString(1) + " vs "+ cur2.getString(6) + " ore "+ cur2.getString(9) + "\n" + cur2.getString(5));
				        			else if (luogo == 2)
				        			ch1_1.setName(cur2.getString(1)+"\n"+cur2.getString(6) + " vs "+ cur3.getString(1) + " ore "+ cur2.getString(9) + "\n" + cur2.getString(5));
				        			else
				        			ch1_1.setName(cur2.getString(1)+"\n"+cur3.getString(1) + " vs "+ cur2.getString(6) + " ore "+ cur2.getString(9) + "\n" + cur2.getString(5));	
				        			ch1_1.setTag(null);
				        			list2.add(ch1_1);
				        	
				        		}
				        		
				        	}
				        gru1.setItems(list2);
				        list2 = new ArrayList<ExpandListChild>();
				        list.add(gru1);
				        cur2.close();
						cur3.close();
					}
						
					}
					cur.close();
					db.close();
			        base.close();
			        return list;
			    }
	
	
}